﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.ComponentModel;

using ecdl.demo.model;
using ecdl.demo.ui.util;

namespace ecdl.demo.ui {
    /// <summary>
    /// Interaction logic for TestRunWindow.xaml
    /// </summary>
    public partial class TestRunWindow : Window, ITestPresenter {

        private Boolean initialized = false;
        
        public TestRunWindow() {
            InitializeComponent();
        }

        private void Window_Activated(object sender, EventArgs e) {
            if (!initialized) {
                initialized = true;
                this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                AppBarFunctions.SetAppBar(this, AppBarPosition.Bottom);
            }
        }

        private void ZoomInClicked(object sender, RoutedEventArgs e) {
            InstructionLabel.FontSize++;
        }

        private void ZoomOutClicked(object sender, RoutedEventArgs e) {
            InstructionLabel.FontSize--;
        }

        private void EndTestClicked(object sender, RoutedEventArgs e) {
            FireEvent(EndClicked);
        }

        private void FireEvent(EventHandler handler) {
            if (handler != null) {
                handler(this, EventArgs.Empty);
            }
        }

        public void StartTest(Test test) {
            
        }

        public event EventHandler SubmitClicked;

        public event EventHandler SkipClicked;

        public event EventHandler EndClicked;

        public void SetProgress(int currentTasks, int numberOfTasks) {
            ProgressLabel.Content = String.Format("Question {0} of {1}", currentTasks, numberOfTasks);
        }

        public TimeSpan TimeLeft {
            set {
                TimeLabel.Dispatcher.Invoke(new Action(
                    delegate() {
                        TimeLabel.Content = value.ToString(@"mm\:ss"); 
                    }
                ));
            }
        }

        public string TaskDescription {
            set {
                string[] delimiters = {"*"};
                String[] items = value.Split(delimiters, StringSplitOptions.None);

                InstructionLabel.Inlines.Clear();
                bool bold = value.StartsWith("*");

                foreach (var item in items) {
                    Inline nextInline = new Run(item);
                    if (bold){
                        //wrap into bold
                        nextInline = new Bold(nextInline);
                    }
                    InstructionLabel.Inlines.Add(nextInline);
                    bold = !bold;
                }
            
            }
        }

        private void SkipButton_Click(object sender, RoutedEventArgs e) {
            FireEvent(SkipClicked);
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e) {
            FireEvent(SubmitClicked);
        }
    }
}
